<?php
class Site extends CI_Controller{

	var $img_path;
	var $img_path_url;


	function __construct() {
		parent::__construct();
		
		$this->img_path = realpath(APPPATH . '../img');
		$this->img_path_url = base_url().'img/';
		
	}
	
	function index(){
		$data['rows'] = $this->site_model->get_entries();

		$data['moods'] = $this->site_model->get_moods();

		$data['categories'] = $this->site_model->get_categories();

		$data['main_content'] = 'site';
		$this->load->view('includes/template', $data);

	}

	function get_entry(){
		$id = $this->uri->segment(3);
		$data['rows']=$this->site_model->get_entry($id);

		$data['main_content'] = 'one_entry';
		$this->load->view('includes/template', $data);
	}

	function __resize_image($image_data){
			$config = array(
			'source_image' => $image_data['full_path'],
			'maintain_ration' => true,
			'width' => 150,
			'height' => 150
			);

			$this->load->library('image_lib', $config);
			$this->image_lib->resize();
	}

	function __do_upload(){
		$config = array(
			'allowed_types' => 'jpg|jpeg|gif|png|bmp',
			'upload_path' => $this->img_path,
			'max_size' => 2000
		);
		$this->load->library('upload', $config);
		$this->upload->do_upload();
		
		return $this->upload->data();
	}

	function send_text_form(){

		$this->load->library('form_validation');
		
		// field name, error message, validation rules
		$this->form_validation->set_rules('headline', 'rubrik', 'trim|required');
		$this->form_validation->set_rules('text', 'text', 'trim|required');
		$this->form_validation->set_rules('mood', 'humör', 'trim|required|is_natural_no_zero');
		$this->form_validation->set_rules('category', 'kategori', 'trim|required|is_natural_no_zero');
		
		if($this->form_validation->run() == FALSE)
		{
			$this->index();
		}
		
		else
		{			
			
			$this->site_model->add_entry();
			$this->index();
		}
	}
	
	function send_image_form(){

		$this->load->library('form_validation');
		
		// field name, error message, validation rules
		$this->form_validation->set_rules('headline', 'rubrik', 'trim|required');
		$this->form_validation->set_rules('mood', 'humör', 'trim|required|is_natural_no_zero');
		$this->form_validation->set_rules('category', 'kategori', 'trim|required|is_natural_no_zero');
		
		if($this->form_validation->run() == FALSE)
		{
			$this->index();
		}
		
		else
		{			
			$image_data = $this->__do_upload();
			var_dump($image_data);
			if($image_data['is_image']){
				$this->__resize_image($image_data);
				$this->site_model->add_entry_image($image_data);
				$this->index();
			}
		}
	}
	

	function search_form(){
			$terms = $this->input->post('search_string');	

			$data['rows'] = $this->site_model->search_entries($terms);
			$data['main_content'] = 'search_results';
			$this->load->view('includes/template', $data);
		}

	function export_xml(){
		if($this->site_model->get_xml()){
			$data['main_content'] = 'add_success';
			$this->load->view('includes/template', $data);
		}
	}

	function import_xml()
		{
			$this->load->view('import_xml');
			
		}

	function edit_entry(){
		$id = $this->uri->segment(3);
		$data['rows']=$this->site_model->get_entry($id);	
		$data['moods'] = $this->site_model->get_moods();
		$data['categories'] = $this->site_model->get_categories();

		$data['main_content'] = 'edit_entry';
		$this->load->view('includes/template', $data);
	}

	function edit_entry_form(){
		
		$this->load->library('form_validation');
		
		// field name, error message, validation rules
		$this->form_validation->set_rules('headline', 'rubrik', 'trim|required');
		$this->form_validation->set_rules('text', 'text', 'trim|required');
		$this->form_validation->set_rules('mood', 'humör', 'trim|required|is_natural_no_zero');
		$this->form_validation->set_rules('category', 'kategori', 'trim|required|is_natural_no_zero');
		
		if($this->form_validation->run() == FALSE)
		{
			$this->edit_entry();
		}
		
		else{
		$id = $this->uri->segment(3);
		if($this->site_model->update_entry($id)){	
			$this->index();
		}
	}
	}
	
	function text() {
		$data['rows'] = $this->site_model->get_entries();

		$data['moods'] = $this->site_model->get_moods();

		$data['categories'] = $this->site_model->get_categories();
		
		$data['add_form'] = 'text_form';
		
		if ($this->input->post('ajax')) {
			$this->load->view($data['add_form'], $data);			
		} else {
			$this->load->view('includes/template', $data);
		}
	}
	
	function image() {
		
		$data['rows'] = $this->site_model->get_entries();

		$data['moods'] = $this->site_model->get_moods();

		$data['categories'] = $this->site_model->get_categories();
		
		$data['add_form'] = 'image_form';
		
		if ($this->input->post('ajax')) {
			$this->load->view($data['add_form'], $data);			
		} else {
			$this->load->view('includes/template', $data);
		}
		
		
	}
}